package com.base.cn.platform.os.controller.course.catalog;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.course.MaterialType;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.course.catalog.CourseCatalogService;
import com.base.cn.platform.os.service.course.cloud.CloudService;
import com.base.cn.platform.os.service.course.cou.CourseService;
import com.base.cn.platform.os.service.manage.config.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 后台课程目录Controller
 *
 * @author s.li
 * @create 2018-04-26-20:00
 */
@Controller
@RequestMapping("/admin/course/catalog")
public class AdminCourseCatalogController extends AdminBaseController {

    @Autowired
    private CourseCatalogService courseCatalogService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private CloudService cloudService;

    /**
     * 初始化课程目录页面
     * @param model
     * @param courseId 课程ID
     * @return 操作页面
     */
    @RequestMapping({"/initCourseCatalog","/initLiveCatalog","/initSmallCatalog"})
    public String initCourseCatalog(Model model,
                                    @RequestParam("courseId")BigDecimal courseId){
        Map<String,Object> course = courseService.findCourseById(courseId,false,false,false, false);
        model.addAttribute("course",course);
        return resultPage("/course/catalog/save_course_catalog");
    }

    /**
     * 加载课程目录数据
     * @param courseId 课程ID
     * @return  Map<String,Object>
     */
    @RequestMapping("/findCourseCatalog")
    @ResponseBody
    public Map<String,Object> findCourseCatalog(@RequestParam("courseId") BigDecimal courseId){
        List<Map<String,Object>> courseCatalogList = courseCatalogService.findCourseCatalogByCourseId(courseId,true, false, false, false);
        return ResultUtil.SUCCESS("课程目录加载成功",courseCatalogList);
    }

    /**
     * 初始化添加/修改目录页面
     * @param model Model
     * @param courseId 课程ID
     * @param id 目录ID
     * @param parentId 父级ID
     * @return 操作页面
     */
    @RequestMapping(value = {"/initUpdate","/initAdd"})
    public String initUpdateOrAdd(Model model,
                                  @RequestParam("courseId") BigDecimal courseId,
                                  @RequestParam(value = "id",required = false,defaultValue = "0") BigDecimal id,
                                  @RequestParam(value = "parentId",required = false,defaultValue = "0") BigDecimal parentId){
        Map<String,Object> catalog = null;
        if(DataUtil.idIsNotNull(id)){
            catalog = courseCatalogService.findCourseCatalogById(id,true, true, true, false);
        }
        if(catalog==null){
            catalog = new HashMap<>();
            catalog.put("courseId",courseId);
            catalog.put("parentId",parentId);
            if(DataUtil.idIsNotNull(parentId)){
                catalog.put("nodeType",2);
            }else{
                catalog.put("nodeType",1);
            }
        }
        Map<String,Object> course = courseService.findCourseById(courseId,false,false,false, false);
        //检查当前直播是那个
        TreeMap<String,String> vlasProviderMap = sysConfigService.getSysConfigContext(ConfigName.VLASPROVIDER);
        if(vlasProviderMap.get("type").equals("CC")){
         //获取回放列表
            Map material = (Map) catalog.get("material");
            if(ObjectUtils.isNotEmpty(material)){
                if(material.get("typeKey").equals(MaterialType.TYPE_KEY_LIVE.getKey())){
                    if(vlasProviderMap.get("type").equals("CC")){
                        List<Map> playback = cloudService.findCCPlaybackPage(material.get("roomId").toString());
                        model.addAttribute("playback",playback);
                    }
                }

            }
        }
        model.addAttribute("course",course);
        model.addAttribute("catalog",catalog);
        model.addAttribute("vlasProviderMap",vlasProviderMap);
        return resultPage("/course/catalog/save_catalog");
    }

    /**
     * 保存目录信息
     * @param catalog 目录信息对象
     * @return Map<String,Object>
     */
    @RequestMapping("/saveCatalog")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.COURSE,action = "保存或修改目录信息")
    public Map<String,Object> saveCatalog(@RequestBody Map<String,Object> catalog){
        return courseCatalogService.saveCourseCatalog(catalog);
    }

    /**
     * 给课程添加模块目录
     * @param courseId 课程ID
     * @param mouldIds 模块ID
     * @return  Map<String,Object>
     */
    @RequestMapping("/saveCourseMould")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.COURSE,action = "给课程添加模块目录")
    public Map<String,Object> saveCourseMould(@RequestParam("courseId")BigDecimal courseId,
                                              @RequestParam("mouldIds") String mouldIds){
        Map<String,Object> resultMap = courseCatalogService.saveCourseMould(courseId,mouldIds);
        return resultMap;
    }

    /**
     * 修改章节的父级归属
     * @param id 章节ID
     * @param parentId 新的父级ID
     * @return  Map<String,Object>
     */
    @RequestMapping("/updateCatalogParent")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.COURSE,action = "修改章节的父级归属")
    public Map<String,Object> updateCatalogParent(@RequestParam("id") BigDecimal id,
                                                  @RequestParam("parentId") BigDecimal parentId){

        return courseCatalogService.updateCourseCatalogParent(id,parentId);
    }

    /**
     * 删除章节数据
     * @param ids 章节ID串
     * @return Map<String,Object>
     */
    @RequestMapping("/deleteCatalog")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.COURSE,action = "删除章节数据")
    public Map<String,Object> deleteCatalog(@RequestParam("ids") String ids){
        return courseCatalogService.deleteCourseCatalogByIds(ids);
    }

    /**
     * 调整节点排序
     * @param currentId 要操作的节点ID
     * @param currentSort 操作的节点的新排序
     * @param oldId 被替换节点的ID
     * @param oldSort 被替换节点的排序
     * @return Map<String,Object>
     */
    @RequestMapping("/updateNodeSort")
    @ResponseBody
    public Map<String,Object> updateNodeSort(@RequestParam("currentId") BigDecimal currentId,
                                             @RequestParam("currentSort") int currentSort,
                                             @RequestParam("oldId") BigDecimal oldId,
                                             @RequestParam("oldSort") int oldSort){
        courseCatalogService.updateNodeSort(currentId,currentSort,oldId,oldSort);
        return ResultUtil.SUCCESS();
    }

    /**
     * 加载课程目录数据 用于随堂练习
     * @param courseId 课程ID
     * @return  Map<String,Object>
     */
    @RequestMapping("/findCourseCatalogPracticeByCourseId")
    @ResponseBody
    public Map<String,Object> findCourseCatalogPracticeByCourseId(@RequestParam("courseId") BigDecimal courseId,@RequestParam("scoreRate") double scoreRate){
        List<Map<String,Object>> courseCatalogList = courseCatalogService.findCourseCatalogPracticeByCourseId(courseId,scoreRate);
        return ResultUtil.SUCCESS("课程目录加载成功",courseCatalogList);
    }


}
